#include "deviceconnectdao.h"
#include "dbutil.h"
#include <iostream>
#ifdef LINUX_DB
#include<stdio.h>
#else
#include <QSqlQuery>
#include <QVariant>
#include <QSqlError>
#include <QSqlDatabase>
#endif
using namespace std;
DeviceConnectDao::DeviceConnectDao()
{
}
list<Deviceconnect *> DeviceConnectDao::getResult(QString str, QString methodName)
{
    list<Deviceconnect *> deviceconnects;
#ifdef LINUX_DB
    char sql[500];
    sprintf(sql, str.toLocal8Bit().data());
    DbUtils dbUtils;
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct conn_result), methodName);
    while (result != NULL)
    {
        conn_result *cr = (conn_result *)result;

        Deviceconnect *conn = new Deviceconnect();
        conn->setCdate(DbUtils::getTimeFromLong(cr->create_time));
        conn->setCid(cr->cid);
        conn->setConnectType(cr->connectType);
        conn->setLeftDeviceId(cr->deviceId);
        conn->setRightDeviceId(cr->downDevice);
        conn->setSubsetMask(cr->subsetMask);
        conn->setSubsetIp(cr->subsetIp);
        conn->setSubsetMask(cr->subsetMask);
        conn->leftInterface = cr->ift;
        conn->rightInterface = cr->downIf;
        deviceconnects.push_back(conn);
        result = dbUtils.getNextRow();
    }
    dbUtils.freeRes();
#endif
    return deviceconnects;
}

list<Deviceconnect *> DeviceConnectDao::getConns(QString subsetIp, QString subsetMask, int deviceId)
{

    QString str("select * from T_CONNECTION where CUSTOMIZED!=2 and  DEVICE_ID = %1 and SUBSET_IP='%2' and SUBSET_MASK='%3' and STATUS!=6" );
    str = str.arg(DbUtils::getStringFormInt(deviceId), subsetIp, subsetMask);
#ifdef LINUX_DB
    return getResult(str, "DeviceConnectDao::getConns");
#else
    list<Deviceconnect *> deviceconnects;
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("DeviceConnectDao::getConns",
                          "SQL:" + str);
    }
    while (query.next())
    {
        Deviceconnect *conn = new Deviceconnect();
        conn->setCid(query.value(0).toInt());
        conn->setLeftDeviceId(query.value(1).toInt());
        conn->setRightDeviceId(query.value(6).toInt());
        deviceconnects.push_back(conn);
    }
    return deviceconnects;
#endif
}
list<Deviceconnect *> DeviceConnectDao::getSubConn(int deviceId)
{

#ifdef LINUX_DB
    QString str("select * from T_CONNECTION CUSTOMIZED!=2 and  where DEVICE_ID =%1 and DOWN_DEVICE!=%2  and SUBSET_IP is null and SUBSET_MASK is null  and STATUS!=6" );
    str.append(" and DEVICE_ID!=DOWN_DEVICE");
    str = str.arg(DbUtils::getStringFormInt(deviceId), DbUtils::getStringFormInt(deviceId));
    return getResult(str, "DeviceConnectDao::getSubConn");
#else
    list<Deviceconnect *> deviceconnects;
    QSqlQuery query;
    QString str("select * from T_CONNECTION where CUSTOMIZED!=2 and  DEVICE_ID = ? and DOWN_DEVICE!=?  and SUBSET_IP is null and SUBSET_MASK is null  and STATUS!=6" );
    str.append(" and DEVICE_ID!=DOWN_DEVICE");
    query.prepare(str);
    query.bindValue(0, deviceId);
    query.bindValue(1, deviceId);
    if (!query.exec())
    {
        DbUtils::ErrorLog("DeviceConnectDao::getSubConn",
                          "SQL:" + str);
    }
    while (query.next())
    {
        Deviceconnect *conn = new Deviceconnect();
        conn->setCid(query.value(0).toInt());
        conn->setLeftDeviceId(query.value(1).toInt());
        conn->setRightDeviceId(query.value(6).toInt());
        deviceconnects.push_back(conn);
    }
    query.clear();
    return deviceconnects;
#endif
}
list<Deviceconnect *> DeviceConnectDao::getConnByOlt(int oltid)
{
    QString str = "select * from T_CONNECTION where DEVICE_ID=%1 and CUSTOMIZED=2";
    str = str.arg(QString::number(oltid));
#ifdef LINUX_DB
    return this->getResult(str, "DeviceConnectDao::getConnByOlt");
#else
    list<Deviceconnect *> deviceconnects;
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("DeviceConnectDao::getConnByOlt",
                          "SQL:" + str);
    }
    while (query.next())
    {
        Deviceconnect *conn = new Deviceconnect();
        conn->setCid(query.value(0).toInt());
        conn->setLeftDeviceId(query.value(1).toInt());
        conn->setSubsetIp(query.value(4).toString());
        conn->setSubsetMask(query.value(5).toString());
        conn->setRightDeviceId(query.value(6).toInt());
        deviceconnects.push_back(conn);
    }
    query.clear();;
    return deviceconnects;
#endif

}

list<Deviceconnect *> DeviceConnectDao::getConnsByDeviceId(int deviceId, int parentId, int secondFlag)
{
    QString str;
    str = "select con.* from T_CONNECTION con left join JYDW_SUBSET js on \
     js.subset_ip=con.subset_ip and js.subset_mask=con.subset_mask and con.device_id=js.device_id where\
     con.SECOND_FLAG=%1 and con.CUSTOMIZED!=2 and \
     ((con.DEVICE_ID = %2 and con.DOWN_DEVICE!=%3) or\
                           (con.DOWN_DEVICE = %4 and con.DEVICE_ID!=%5)) and con.STATUS!=6";
    str.append(" and con.DEVICE_ID!=con.DOWN_DEVICE");
    str = str.arg(DbUtils::getStringFormInt(secondFlag));
    str = str.arg(DbUtils::getStringFormInt(deviceId)).arg(DbUtils::getStringFormInt(parentId));
    str = str.arg(DbUtils::getStringFormInt(deviceId)).arg(DbUtils::getStringFormInt(parentId));
#ifdef LINUX_DB
    return this->getResult(str, "DeviceConnectDao::getConnsByDeviceId");
#else
    list<Deviceconnect *> deviceconnects;
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("DeviceConnectDao::getConnsByDeviceId",
                          "SQL:" + str);
    }
    while (query.next())
    {
        Deviceconnect *conn = new Deviceconnect();
        conn->setCid(query.value(0).toInt());
        conn->setLeftDeviceId(query.value(1).toInt());
        conn->setSubsetIp(query.value(4).toString());
        conn->setSubsetMask(query.value(5).toString());
        conn->setRightDeviceId(query.value(6).toInt());
        deviceconnects.push_back(conn);
    }
    query.clear();;
    return deviceconnects;
#endif
}

list<Deviceconnect *> DeviceConnectDao::getConnsByDeviceId(QString subsetIp, QString subsetMask, int deviceId,
        int parentid, int secondFlag)
{
    QString str;
    if (subsetIp != "")
    {
        str = "select * from T_CONNECTION where SECOND_FLAG=%1 and CUSTOMIZED!=2 and  DEVICE_ID = %2  and STATUS!=6";
        str.append(" and DEVICE_ID!=DOWN_DEVICE");
        str.append(" and SUBSET_IP='");
        str.append(subsetIp);
        str.append("' and SUBSET_MASK='");
        str.append(subsetMask);
        str.append("' ");
        str = str.arg(DbUtils::getStringFormInt(secondFlag));
        str = str.arg(DbUtils::getStringFormInt(deviceId));
    }
    else
    {
        str = "select * from T_CONNECTION where SECOND_FLAG=%1 and  CUSTOMIZED!=2 and  ((DEVICE_ID = %2 and DOWN_DEVICE!=%3) or\
                (DOWN_DEVICE = %4 and DEVICE_ID!=%5)) and SUBSET_IP is null and SUBSET_MASK is null \
                and STATUS!=6";
        str.append(" and DEVICE_ID!=DOWN_DEVICE");
        str = str.arg(DbUtils::getStringFormInt(secondFlag), DbUtils::getStringFormInt(deviceId),
                      DbUtils::getStringFormInt(parentid)
                      , DbUtils::getStringFormInt(deviceId), DbUtils::getStringFormInt(parentid));
    }
#ifdef LINUX_DB
    return this->getResult(str, "DeviceConnectDao::getConnsByDeviceId");
#else
    list<Deviceconnect *> deviceconnects;
    if (DbUtils::isDbOpen())
    {
        QSqlQuery query;
        query.prepare(str);
        if (!query.exec())
        {
            DbUtils::ErrorLog("DeviceConnectDao::getConnsByDeviceId",
                              "SQL:" + str);
        }
        while (query.next())
        {
            Deviceconnect *conn = new Deviceconnect();
            conn->setCid(query.value(0).toInt());
            conn->setLeftDeviceId(query.value(1).toInt());
            conn->setSubsetIp(query.value(4).toString());
            conn->setSubsetMask(query.value(5).toString());
            conn->setRightDeviceId(query.value(6).toInt());
            deviceconnects.push_back(conn);
        }
        query.clear();
    }

    return deviceconnects;
#endif
}
list<Deviceconnect *> DeviceConnectDao::getSingleEponConn()
{
    QString  str("select * from T_CONNECTION where CUSTOMIZED!=2 and  CONNECT_TYPE = 5 and STATUS!=6");
#ifdef LINUX_DB
    return this->getResult(str, "DeviceConnectDao::getSingleEponConn");
#else
    list<Deviceconnect *> deviceconnects;
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("DeviceConnectDao::getSingleEponConn",
                          "SQL:" + str);
    }
    while (query.next())
    {
        Deviceconnect *conn = new Deviceconnect;
        conn->setCid(query.value(0).toInt());
        conn->setLeftDeviceId(query.value(1).toInt());
        conn->setRightDeviceId(query.value(6).toInt());
        deviceconnects.push_back(conn);
    }
    query.clear();;
    return deviceconnects;
#endif
}
list<Deviceconnect *> DeviceConnectDao::getHandEponConn()
{
    list<Deviceconnect *> result;
    return result;
}
list<Deviceconnect *> DeviceConnectDao::setTrace(int device_id)
{
    list<Deviceconnect *> result;
    QString str = "select * from T_CONNECTION where CUSTOMIZED!=2 and  DOWN_DEVICE =%1";
    str = str.arg(DbUtils::getStringFormInt(device_id));
#ifdef LINUX_DB
    result = this->getResult(str, "DeviceConnectDao::setTrace");
#else
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("DeviceConnectDao::setTrace",
                          "SQL:" + str);
    }
    while (query.next())
    {
        Deviceconnect *device = new Deviceconnect;
        device->setLeftDeviceId(query.value(1).toInt());
        device->setSubsetIp(query.value(4).toString());
        device->setSubsetMask(query.value(5).toString());
        result.push_back(device);
    }
    query.clear();

#endif
    return result;
}
list<Deviceconnect *> DeviceConnectDao::getUnderDbSubset(int bsid, QString subset1, QString subset2)
{
    struct device_struct
    {
        char ip[16];
        int down_device;
    };
    list<Deviceconnect *> result;
    QString qSql = "select subset_ip,down_device from T_CONNECTION where CUSTOMIZED!=2 and  \
            down_device in(select device_id from t_device where \
                           device_id in ( select distinct device_id from jydw_interface_ip where bisubset_id=%1)  and (STATUS =1 or STATUS=2))";
    qSql.append(" and (subset_ip='%2' or subset_ip='%3')");
    qSql = qSql.arg(DbUtils::getStringFormInt(bsid), subset1, subset2);
#ifdef LINUX_DB
    DbUtils dbUtils;
    char sql[500];
    sprintf(sql, qSql.toLocal8Bit().data());
    MYSQL_ROW result1 = dbUtils.startQuery(sql, sizeof(struct device_struct), "DeviceConnectDao::getUnderDbSubset");
    while (result1 != NULL)
    {
        device_struct *p_result = (device_struct *)result1;
        Deviceconnect *device = new Deviceconnect;
        device->setSubsetIp(p_result->ip);
        device->setRightDeviceId(p_result->down_device);
        result.push_back(device);
        result1 = dbUtils.getNextRow();
    }
    dbUtils.freeRes();
#else

    QSqlQuery query;
    query.prepare(qSql);
    if (!query.exec())
    {
        DbUtils::ErrorLog("DeviceConnectDao::getUnderDbSubset",
                          "SQL:" + qSql);
    }
    while (query.next())
    {
        Deviceconnect *device = new Deviceconnect;
        device->setSubsetIp(query.value(0).toString());
        device->setRightDeviceId(query.value(1).toInt());
        result.push_back(device);
    }
    query.clear();
#endif
    return result;
}
list<Deviceconnect *> DeviceConnectDao::getConnBy2DeviceId(int deviceid1, int deviceid2)
{
    QString qsql =
        "select * from T_CONNECTION where (device_id=%1 and down_device=%2) or (device_id=%3 and down_device=%4)";
    qsql = qsql.arg(QString::number(deviceid1), QString::number(deviceid2)
                    , QString::number(deviceid2), QString::number(deviceid1));


    list<Deviceconnect *> result;
#ifdef LINUX_DB
    result = this->getResult(qsql, "DeviceConnectDao::getConnBy2DeviceId");
#else
    QSqlQuery query;
    query.prepare(qsql);
    if (!query.exec())
    {
        DbUtils::ErrorLog("DeviceConnectDao::getConnBy2DeviceId",
                          "SQL:" + qsql);
    }
    while (query.next())
    {
        Deviceconnect *conn = new Deviceconnect;
        conn->setCid(query.value(0).toInt());
        conn->setLeftDeviceId(query.value(1).toInt());
        conn->leftInterface = query.value(2).toInt();
        conn->rightInterface = query.value(7).toInt();
        conn->setRightDeviceId(query.value(6).toInt());
        conn->setConnectType(query.value(9).toInt());
        result.push_back(conn);
    }
    query.clear();

#endif
    return result;
}

//list<Deviceconnect*> DeviceConnectDao::getDownDeviceByDeviceId(int device_id){
//    QString qsql = "select down_device from T_CONNECTION where device_id=%1";
//    qsql = qsql.arg(QString::number(device_id));
//    list<Deviceconnect*> result;
//#ifdef LINUX_DB
//    result = this->getResult(qsql,"DeviceConnectDao::getDownDeviceByDeviceId");
//#else
//    QSqlQuery query;
//    query.prepare(qsql);
//    if(!query.exec()){
//        DbUtils::ErrorLog("DeviceConnectDao::getDownDeviceByDeviceId",
//                          "SQL:" + qsql);
//    }
//    while(query.next()){
//        Deviceconnect *conn = new Deviceconnect;
//        conn->setCid(query.value(0).toInt());
//        conn->setLeftDeviceId(query.value(1).toInt());
//        conn->leftInterface = query.value(2).toInt();
//        conn->rightInterface = query.value(7).toInt();
//        conn->setRightDeviceId(query.value(6).toInt());
//        conn->setConnectType(query.value(9).toInt());
//        result.push_back(conn);
//    }
//    query.clear();

//#endif
//    return result;
//}
